#include "DataManager.h"
#include "Namespace.h"
#include "SystemHelper.h"

#include <QCoreApplication>
#include <QFileDialog>
#include <QStandardPaths>
#include <QTranslator>

const QChar ZH_CN_UTFCODE[] = {0x7B80, 0x4F53, 0x4E2D, 0x6587};
const QChar ZH_CHT_UTFCODE[] = {0x7E41, 0x9AD4, 0x4E2D, 0x6587};
const QChar JA_JP_UTFCODE[] = {0x65E5, 0x672C, 0x8A9E};

#define Q_FROM_UTFCODE(Name)                                                                       \
    QString::fromRawData(Name##_UTFCODE, sizeof(Name##_UTFCODE) / sizeof(QChar))

Q_SINGLETON_DECLARE(DataManager)

QMap<QString, QString> DataManager::lastOpenPaths;

DataManager::DataManager(QObject *parent) : BaseManager(parent) {
    construct();
    reloadStrings();
}

DataManager::~DataManager() {
}

bool DataManager::load() {
    return true;
}

bool DataManager::save() {
    return true;
}

void DataManager::reloadStrings() {
    MainTitle = My::AppName;
    WindowTitle = MainTitle + QString(" %1").arg(My::Version);
    UntitledPrefix = "*";

    ErrorTitle = tr("Error");
    UntitledFileName = tr("Untitled");
    DeletedPrefix = tr("(Deleted)");

#ifdef Q_OS_WINDOWS
    fileManagerName = tr("Explorer");
#elif defined(Q_OS_MAC)
    fileManagerName = tr("Finder");
#else
    fileManagerName = tr("File Manager");
#endif

    emit stringUpdated();
}

bool DataManager::translate(const QString &filename) {
    QTranslator *t = new QTranslator(this);
    if (t->load(filename)) {
        qApp->installTranslator(t);
        Translators.insert(t);
        reloadStrings();
        return true;
    }
    delete t;
    return false;
}

void DataManager::eliminate() {
    for (auto it = Translators.begin(); it != Translators.end(); ++it) {
        auto t = *it;
        qApp->removeTranslator(t);
        delete t;
    }
    Translators.clear();
}

void DataManager::loadLocale(int index) {
}

int DataManager::localeCount() const {
    return 1;
}

QStringList DataManager::localeNames() const {
    QStringList list = {
        "English",              // English
        Q_FROM_UTFCODE(ZH_CN),  // 简体中文
        Q_FROM_UTFCODE(ZH_CHT), // 繁體中文
        Q_FROM_UTFCODE(JA_JP),  // 日本語
    };
    return list;
}

QString DataManager::tempDir() {
    return QDir::tempPath();
}

QString DataManager::desktopDir() {
    return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
}

QString DataManager::openDir(const QString &title, const QString &flag) {
    QString path = QFileDialog::getExistingDirectory(nullptr, title, getLastOpenPath(flag));
    if (!path.isEmpty()) {
        saveLastOpenDir(flag, path);
    }
    return path;
}

QString DataManager::getLastOpenPath(const QString &type) {
    auto it = lastOpenPaths.find(type);
    if (it == lastOpenPaths.end()) {
        it = lastOpenPaths.insert(type, desktopDir());
    } else if (!Sys::isDirExist(it.value())) {
        it.value() = desktopDir();
    }
    return it.value();
}

void DataManager::saveLastOpenDir(const QString &type, const QString &path) {
    lastOpenPaths.insert(type, Sys::PathFildDirPath(path));
}
